﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace WindowsTest
{
    /// <summary>
    /// XML解析器
    /// </summary>
    class XmlParser
    {
        /// <summary>
        /// 读取并解析XML文件
        /// </summary>
        /// <param name="path">要解析的XML文件所在目录</param>
        /// <returns>解析结果</returns>
        public static List<XmlDetails> ReadXmlFile(string path)
        {
            var xmlDetails = new List<XmlDetails>();
            var dir = new DirectoryInfo(path);
            FileInfo[] finfos = dir.GetFiles();
            if (finfos.Any())
            {
                foreach (var fileInfo in finfos)
                {
                    try
                    {
                        if (isXml(fileInfo.Extension))
                        {
                            var xd = parseXmlFile(fileInfo.FullName);
                            xd.FileName = fileInfo.Name;
                            xmlDetails.Add(xd);
                        }
                    }
                    catch (Exception)
                    {

                    }
                }
            }
            return xmlDetails;
        }

        /// <summary>
        /// 判断是否是XML文件
        /// </summary>
        /// <param name="ext">文件名后缀</param>
        /// <returns>是否是XML文件</returns>
        private static bool isXml(string ext)
        {
            if (ext.ToUpper() == ".XML")
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 解析XML文件
        /// </summary>
        /// <param name="path">要解析的XML文件路径</param>
        /// <returns>解析结果</returns>
        private static XmlDetails parseXmlFile(string path)
        {
            var xd = new XmlDetails();
            xd.DicMedicine = new Dictionary<int, List<string>>();

            var doc = new XmlDocument();
            doc.Load(path);

            //使用xpath表达式选择文档中所有的student子节点
            XmlNode root = doc.SelectSingleNode("root");
            if (root != null)
            {
                XmlNodeList receipts = root.SelectNodes("receipt");
                if (receipts != null)
                {
                    xd.ImageCount = receipts.Count;//图片数
                    var n = 0;
                    var mCount = 0;
                    foreach (XmlNode receipt in receipts)
                    {
                        var fieldValues = receipt.SelectSingleNode("fieldValues");
                        if (fieldValues != null)
                        {
                            var valueStr = fieldValues.InnerText;
                            var strNode = valueStr.Split(';');
                            foreach (var s in strNode)
                            {
                                var names = s.Split('=');
                                foreach (var name in names)
                                {
                                    if (name == "detail_name")
                                    {
                                        if(names[1] != null && names[1] != "")
                                        {
                                            var ms = names[1].Split('|');
                                            mCount += ms.Count();
                                            var listMs = new List<string>();
                                            foreach (var m in ms)
                                            {
                                                listMs.Add(m);
                                            }
                                            xd.DicMedicine.Add(n, listMs);//添加药品字典
                                        }
                                    }
                                }
                            }
                        }
                        n++;
                    }
                    xd.MedicineCount = mCount;
                }
            }
            return xd;
        }
    }

    /// <summary>
    /// XML详情描述类
    /// </summary>
    public class XmlDetails
    {
        /// <summary>
        /// 被解析XML文件名称
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// 图片数
        /// </summary>
        public int ImageCount { get; set; }

        /// <summary>
        /// 药品数
        /// </summary>
        public int MedicineCount { get; set; }

        /// <summary>
        /// 药品字典，按图片分索引
        /// </summary>
        public Dictionary<int, List<string>> DicMedicine { get; set; }
    }
}
